﻿//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Reflection;
//using System.Web.Mvc;

//using Umbraco.Framework.Localization;

//using Newtonsoft.Json;

//using MVCBootstrap.DataProvider;
//using MVCBootstrap.Membership;

//using mvcForum.Core;
//using mvcForum.Core.Abstractions.Interfaces;
//using mvcForum.Core.Interfaces.Events;

//using mvcForum.Web.Controllers;
//using MVCBootstrap.AsyncHandling;
//using CreativeMinds.MVC.Common.Filters;

//namespace mvcForum.Web.Areas.Forum.Controllers {

//    public class AsyncController : ForumBaseController {
//        private readonly IConfiguration config;
//        private readonly TextManager textManager;
//        private readonly ILog logger;
//        private readonly IEnumerable<IEventListener> listeners;

//        public AsyncController(IUserProvider userProvider,
//                                IContext context,
//                                IConfiguration config,
//                                ILog logger,
//                                TextManager textManager,
//                                IEnumerable<IEventListener> listeners)
//            : base(userProvider, context) {

//            this.config = config;
//            this.logger = logger;
//            this.textManager = textManager;
//            this.listeners = listeners;
//        }

//        [OnlyLocalCalls]
//        public String Execute(String listener, String data, String type, String assembly) {
//            this.logger.Log(EventType.Debug, String.Format("Async Controller called, {0} {1} {2} {3}", listener, data, type, assembly));
//            try {
//                Assembly ass = Assembly.Load(assembly);
//                Type t = ass.GetType(type);

//                TextReader reader = new StringReader(data);

//                JsonSerializer seri = new JsonSerializer();
//                Object payload = seri.Deserialize(new JsonTextReader(reader), t);

//                Type listenerGeneric = typeof(IEventListener<>);

//                foreach (IEventListener l in this.listeners) {
//                    if (l.GetType().FullName == listener) {
//                        l.Handle(payload);
//                    }
//                }
//            }
//            catch (Exception ex) {
//                this.logger.Log(EventType.Fatal, "Async controller", ex);
//            }
//            // TODO:
//            return "";
//        }

//        //[OnlyLocalCalls]
//        //public ActionResult MessageAdded(Int32 postId) {
//        //    logger.Log(EventType.Debug, String.Format("Async called arrived, MessageAdded, Id {0}, from IP {1}", postId, this.Request.UserHostAddress));
//        //    try {
//        //        // Get the message.
//        //        var post = this.GetRepository<Post>().Read(postId);

//        //        logger.Log(EventType.Debug, String.Format("Found {0} followers", post.Topic.Followers.Count));

//        //        // Iterate the followers.
//        //        foreach (var ft in post.Topic.Followers) {
//        //            // No need to inform the author himself!
//        //            if (ft.User.Id != post.Author.Id) {
//        //                // Does he have an e-mail address on his account?
//        //                if (!String.IsNullOrWhiteSpace(ft.User.Email)) {
//        //                    logger.Log(EventType.Debug, String.Format("User {0} ({1}) is following topic {2}.", ft.User.Name, ft.User.Email, post.Topic.Title));

//        //                    //
//        //                    // TODO: Put this in a queue?? Do a daily digest! Optional?
//        //                    //

//        //                    MailMessage message = new MailMessage();
//        //                    // Get the "from address" and "from name" from the configuration.
//        //                    message.From = new MailAddress(config.RobotEmailAddress, config.RobotName);
//        //                    // And let's put the user's e-mail address in the to field.
//        //                    message.To.Add(new MailAddress(ft.User.Email));
//        //                    // UTF8 FTW!
//        //                    message.SubjectEncoding = Encoding.UTF8;
//        //                    message.BodyEncoding = Encoding.UTF8;

//        //                    // Get the user settings, we'll be localising the text.
//        //                    ForumUserSettings userSettings = post.Author.Settings();
//        //                    // Create a LanguageInfo instance that matches the user's settings.
//        //                    var userLI = new LanguageInfo { Culture = new CultureInfo(userSettings.Culture), Key = userSettings.Culture /*, Description = "English (UK)"*/ };
//        //                    // Get the subject from the localisation system.
//        //                    message.Subject = this.textManager.Get<AsyncController>("FollowTopic.Subject", language: userLI, values: new { Topic = post.Topic, Post = post });

//        //                    // Let's create the URL for view topic.
//                            //String actionURL = Url.RouteUrl("ShowTopic", new RouteValueDictionary { { "id", post.Topic.Id }, { "title", Url.URLFriendlyString(post.Topic.Title) } });
//        //                    // Get the site's URL from the configuration.
//        //                    String siteURL = config.SiteURL;
//        //                    if (siteURL.EndsWith("/")) {
//        //                        siteURL = siteURL.Substring(0, siteURL.Length - 1);
//        //                    }
//        //                    // Merge site URL and view topic URL.
//        //                    String url = String.Format("{0}{1}", siteURL, actionURL);

//        //                    // Get the content of the body from the localisation system!
//        //                    message.Body = this.textManager.Get<AsyncController>("FollowTopic.Body", language: userLI, values: new { Topic = post.Topic, Post = post, URL = url });
//        //                    message.IsBodyHtml = false;

//        //                    try {
//        //                        // Create a SMTP client, this will use the settings from the web.config file.
//        //                        SmtpClient client = new SmtpClient();
//        //                        // Send the message.
//        //                        client.Send(message);

//        //                        logger.Log(EventType.Debug, String.Format("The message to user {0} ({1}) has been sent.", post.Author.Name, post.Author.Email));
//        //                    }
//        //                    catch (Exception ex) {
//        //                        logger.Log(EventType.Error, String.Format("Could not send e-mail to {0} from AsyncController, MessageAdded", post.Author.Email), ex);
//        //                    }
//        //                }
//        //            }
//        //        }
//        //    }
//        //    catch (Exception ex) {
//        //        logger.Log(EventType.Error, "AsyncController, MessageAdded", ex);
//        //    }

//        //    return View();
//        //}
//    }
//}